#!/bin/bash

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 默认参数
OUTPUT_FILE="disk_static_info_$(date +%Y%m%d_%H%M%S).txt"
TARGET_DISK=""  # 目标磁盘，默认为空（收集所有磁盘信息）

# 帮助信息
show_help() {
    echo "用法: $0 [选项]"
    echo "选项:"
    echo "  -o, --output <文件>   输出文件（默认: disk_static_info_时间戳.txt）"
    echo "  -t, --target <磁盘>   目标磁盘（例如: sda, nvme0n1），默认为所有磁盘"
    echo "  -h, --help           显示此帮助信息"
    exit 0
}

# 解析命令行参数
while [[ $# -gt 0 ]]; do
    case $1 in
        -o|--output)
            OUTPUT_FILE="$2"
            shift 2
            ;;
        -t|--target)
            TARGET_DISK="$2"
            shift 2
            ;;
        -h|--help)
            show_help
            ;;
        *)
            echo "未知选项: $1"
            show_help
            ;;
    esac
done

# 收集磁盘静态信息
collect_disk_static_info() {
    echo "=== Linux 磁盘静态信息 ===" > "$OUTPUT_FILE"
    echo "收集时间: $(date)" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    
    # 磁盘基本信息
    echo -e "\n1. 磁盘基本信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "系统磁盘列表:" >> "$OUTPUT_FILE"
    lsblk -d -o NAME,SIZE,TYPE,MOUNTPOINT >> "$OUTPUT_FILE"
    
    # 磁盘详细信息
    echo -e "\n2. 磁盘详细信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘详细信息:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            echo "磁盘: $disk" >> "$OUTPUT_FILE"
            lsblk -d -o NAME,SIZE,TYPE,MOUNTPOINT,FSTYPE,MODEL,ROTA,STATE /dev/$disk >> "$OUTPUT_FILE"
            echo "" >> "$OUTPUT_FILE"
        fi
    done
    
    # 磁盘分区信息
    echo -e "\n3. 磁盘分区信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘分区信息:" >> "$OUTPUT_FILE"
    fdisk -l | grep -A 5 "Disk /dev/" >> "$OUTPUT_FILE"
    
    # 磁盘调度器信息
    echo -e "\n4. 磁盘调度器信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘调度器信息:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            if [ -f "/sys/block/$disk/queue/scheduler" ]; then
                echo "磁盘 $disk 调度器: $(cat /sys/block/$disk/queue/scheduler)" >> "$OUTPUT_FILE"
            fi
        fi
    done
    
    # 磁盘高级特性
    echo -e "\n5. 磁盘高级特性:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    
    # 检查是否支持TRIM
    echo "TRIM支持:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            if [ -f "/sys/block/$disk/queue/discard_granularity" ]; then
                discard_gran=$(cat /sys/block/$disk/queue/discard_granularity)
                if [ "$discard_gran" -gt 0 ]; then
                    echo "  $disk: 支持TRIM (粒度: $discard_gran 字节)" >> "$OUTPUT_FILE"
                else
                    echo "  $disk: 不支持TRIM" >> "$OUTPUT_FILE"
                fi
            else
                echo "  $disk: 无法确定TRIM支持" >> "$OUTPUT_FILE"
            fi
        fi
    done
    echo "" >> "$OUTPUT_FILE"
    
    # 检查是否支持NCQ
    echo "NCQ支持:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            if [ -f "/sys/block/$disk/device/queue_depth" ]; then
                queue_depth=$(cat /sys/block/$disk/device/queue_depth)
                echo "  $disk: 队列深度 $queue_depth" >> "$OUTPUT_FILE"
            else
                echo "  $disk: 无法确定NCQ支持" >> "$OUTPUT_FILE"
            fi
        fi
    done
    echo "" >> "$OUTPUT_FILE"
    
    # 检查是否支持多队列
    echo "多队列支持:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            if [ -d "/sys/block/$disk/mq" ]; then
                echo "  $disk: 支持多队列" >> "$OUTPUT_FILE"
            else
                echo "  $disk: 不支持多队列" >> "$OUTPUT_FILE"
            fi
        fi
    done
    echo "" >> "$OUTPUT_FILE"
    
    # 检查是否支持电源管理
    echo "电源管理支持:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            if [ -f "/sys/block/$disk/queue/nomerges" ]; then
                echo "  $disk: 支持电源管理" >> "$OUTPUT_FILE"
            else
                echo "  $disk: 无法确定电源管理支持" >> "$OUTPUT_FILE"
            fi
        fi
    done
    echo "" >> "$OUTPUT_FILE"
    
    # 磁盘IO统计信息
    echo -e "\n6. 磁盘IO统计信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘IO统计信息:" >> "$OUTPUT_FILE"
    iostat -x -d 1 1 >> "$OUTPUT_FILE"
    
    # 磁盘SMART信息
    echo -e "\n7. 磁盘SMART信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘SMART信息:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            if command -v smartctl &>/dev/null; then
                echo "磁盘 $disk SMART信息:" >> "$OUTPUT_FILE"
                smartctl -a /dev/$disk | grep -E "Model|Serial|Firmware|User Capacity|Sector Size|Rotation Rate|ATA Version|SMART support|Temperature|Power_On_Hours|Reallocated_Sector_Ct|Current_Pending_Sector|Offline_Uncorrectable" >> "$OUTPUT_FILE" 2>/dev/null
                echo "" >> "$OUTPUT_FILE"
            else
                echo "无法获取SMART信息: smartctl命令不可用" >> "$OUTPUT_FILE"
                break
            fi
        fi
    done
    
    # 磁盘IO调度器参数
    echo -e "\n8. 磁盘IO调度器参数:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘IO调度器参数:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            echo "磁盘 $disk IO调度器参数:" >> "$OUTPUT_FILE"
            if [ -d "/sys/block/$disk/queue/iosched" ]; then
                for param in /sys/block/$disk/queue/iosched/*; do
                    if [ -f "$param" ]; then
                        param_name=$(basename "$param")
                        param_value=$(cat "$param" 2>/dev/null)
                        echo "  $param_name: $param_value" >> "$OUTPUT_FILE"
                    fi
                done
            else
                echo "  无法获取IO调度器参数" >> "$OUTPUT_FILE"
            fi
            echo "" >> "$OUTPUT_FILE"
        fi
    done
    
    # 磁盘性能参数
    echo -e "\n9. 磁盘性能参数:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘性能参数:" >> "$OUTPUT_FILE"
    for disk in $(lsblk -d -o NAME | grep -v NAME); do
        if [ -z "$TARGET_DISK" ] || [ "$disk" = "$TARGET_DISK" ]; then
            echo "磁盘 $disk 性能参数:" >> "$OUTPUT_FILE"
            
            # 检查是否支持多队列
            if [ -d "/sys/block/$disk/mq" ]; then
                echo "  多队列支持: 是" >> "$OUTPUT_FILE"
                # 获取多队列参数
                if [ -f "/sys/block/$disk/mq/0/cpu_mask" ]; then
                    echo "  CPU掩码: $(cat /sys/block/$disk/mq/0/cpu_mask)" >> "$OUTPUT_FILE"
                fi
            else
                echo "  多队列支持: 否" >> "$OUTPUT_FILE"
            fi
            
            # 获取请求队列参数
            if [ -f "/sys/block/$disk/queue/nr_requests" ]; then
                echo "  请求队列长度: $(cat /sys/block/$disk/queue/nr_requests)" >> "$OUTPUT_FILE"
            fi
            
            if [ -f "/sys/block/$disk/queue/read_ahead_kb" ]; then
                echo "  预读大小: $(cat /sys/block/$disk/queue/read_ahead_kb) KB" >> "$OUTPUT_FILE"
            fi
            
            if [ -f "/sys/block/$disk/queue/nomerges" ]; then
                echo "  合并请求: $(cat /sys/block/$disk/queue/nomerges)" >> "$OUTPUT_FILE"
            fi
            
            if [ -f "/sys/block/$disk/queue/add_random" ]; then
                echo "  添加随机性: $(cat /sys/block/$disk/queue/add_random)" >> "$OUTPUT_FILE"
            fi
            
            echo "" >> "$OUTPUT_FILE"
        fi
    done
    
    # 磁盘RAID信息
    echo -e "\n10. 磁盘RAID信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘RAID信息:" >> "$OUTPUT_FILE"
    
    # 检查mdadm命令是否可用
    if command -v mdadm &>/dev/null; then
        mdadm --detail --scan >> "$OUTPUT_FILE" 2>/dev/null
        echo "" >> "$OUTPUT_FILE"
        
        # 检查每个RAID设备
        for md in /dev/md*; do
            if [ -b "$md" ]; then
                echo "RAID设备 $md 详细信息:" >> "$OUTPUT_FILE"
                mdadm --detail "$md" >> "$OUTPUT_FILE" 2>/dev/null
                echo "" >> "$OUTPUT_FILE"
            fi
        done
    else
        echo "无法获取RAID信息: mdadm命令不可用" >> "$OUTPUT_FILE"
    fi
    
    # 磁盘IO统计信息（更详细）
    echo -e "\n11. 磁盘IO统计信息（详细）:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    echo "磁盘IO统计信息（详细）:" >> "$OUTPUT_FILE"
    
    # 检查iostat命令是否可用
    if command -v iostat &>/dev/null; then
        iostat -xz 1 1 >> "$OUTPUT_FILE"
    else
        echo "无法获取详细IO统计信息: iostat命令不可用" >> "$OUTPUT_FILE"
    fi
    
    # 内核参数信息
    echo -e "\n12. 内核参数信息:" >> "$OUTPUT_FILE"
    echo "----------------------------------------" >> "$OUTPUT_FILE"
    
    # 检查sysctl命令是否可用
    if command -v sysctl &>/dev/null; then
        # 磁盘请求队列参数
        echo "磁盘请求队列参数:" >> "$OUTPUT_FILE"
        nr_requests=$(sysctl -n block.nr_requests 2>/dev/null)
        if [ -n "$nr_requests" ]; then
            echo "  block/nr_requests: $nr_requests" >> "$OUTPUT_FILE"
        else
            echo "  block/nr_requests: 128 (默认值)" >> "$OUTPUT_FILE"
        fi
        
        read_ahead_kb=$(sysctl -n block.read_ahead_kb 2>/dev/null)
        if [ -n "$read_ahead_kb" ]; then
            echo "  block/read_ahead_kb: $read_ahead_kb" >> "$OUTPUT_FILE"
        else
            echo "  block/read_ahead_kb: 128 (默认值，单位KB)" >> "$OUTPUT_FILE"
        fi
        echo "" >> "$OUTPUT_FILE"
        
        # 系统缓存参数
        echo "系统缓存参数:" >> "$OUTPUT_FILE"
        echo "  vm.dirty_ratio: $(sysctl -n vm.dirty_ratio 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.dirty_background_ratio: $(sysctl -n vm.dirty_background_ratio 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.dirty_expire_centisecs: $(sysctl -n vm.dirty_expire_centisecs 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.dirty_writeback_centisecs: $(sysctl -n vm.dirty_writeback_centisecs 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
        
        # 虚拟内存参数
        echo "虚拟内存参数:" >> "$OUTPUT_FILE"
        echo "  vm.swappiness: $(sysctl -n vm.swappiness 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.vfs_cache_pressure: $(sysctl -n vm.vfs_cache_pressure 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
        
        # 文件系统参数
        echo "文件系统参数:" >> "$OUTPUT_FILE"
        echo "  fs.file-max: $(sysctl -n fs.file-max 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  fs.nr_open: $(sysctl -n fs.nr_open 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
        
        # 文件系统缓存参数
        echo "文件系统缓存参数:" >> "$OUTPUT_FILE"
        echo "  vm.dirty_ratio: $(sysctl -n vm.dirty_ratio 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.dirty_background_ratio: $(sysctl -n vm.dirty_background_ratio 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.dirty_expire_centisecs: $(sysctl -n vm.dirty_expire_centisecs 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.dirty_writeback_centisecs: $(sysctl -n vm.dirty_writeback_centisecs 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
        
        # 虚拟内存参数
        echo "虚拟内存参数:" >> "$OUTPUT_FILE"
        echo "  vm.swappiness: $(sysctl -n vm.swappiness 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "  vm.vfs_cache_pressure: $(sysctl -n vm.vfs_cache_pressure 2>/dev/null || echo "不可用")" >> "$OUTPUT_FILE"
        echo "" >> "$OUTPUT_FILE"
    else
        echo "无法获取内核参数信息: sysctl命令不可用" >> "$OUTPUT_FILE"
        echo "磁盘请求队列参数默认值:" >> "$OUTPUT_FILE"
        echo "  block/nr_requests: 128 (默认值)" >> "$OUTPUT_FILE"
        echo "  block/read_ahead_kb: 128 (默认值，单位KB)" >> "$OUTPUT_FILE"
    fi
    
    echo -e "\n静态信息收集完成，已保存到: $OUTPUT_FILE"
}

# 收集磁盘静态信息
echo -e "${BLUE}正在收集磁盘静态信息...${NC}"
collect_disk_static_info

echo -e "${GREEN}磁盘静态信息收集完成，已保存到: $OUTPUT_FILE${NC}" 